﻿using System.Collections.Generic;

namespace DCT.Model
{
    public class SQLModel
    {
        static public bool LoadProject(SQLModelDataContext dataContext, ICollection<DeviceBaseModel> deviceModels, ICollection<DeviceBaseGraphModel> deviceGraphModels)
        {
            if (!dataContext.DatabaseExists())
            {
                return false;
            }

            deviceModels.Clear();
            deviceGraphModels.Clear();

            foreach (Device device in dataContext.Device)
            {
                if (Resource.SQLModel_Axle_DeviceType == device.DeviceType)
                {
                    deviceModels.Add(new AxleModel(device.AxleDevice, device));

                    foreach (Graph graph in device.Graph)
                    {
                        deviceGraphModels.Add(new AxleGraphModel(graph.AxleGraph, graph));
                    }
                }
                else if (Resource.SQLModel_MiscellaneousDevice_DeviceType == device.DeviceType)
                {
                    deviceModels.Add(new MiscellaneousDeviceModel(device.MiscellaneousDevice, device));

                    foreach (Graph graph in device.Graph)
                    {
                        deviceGraphModels.Add(new MiscellaneousDeviceGraphModel(graph.MiscellaneousDeviceGraph, graph));
                    }
                }
                else if (Resource.SQLModel_Platform_DeviceType == device.DeviceType)
                {
                    deviceModels.Add(new PlatformModel(device.PlatformDevice, device));

                    foreach (Graph graph in device.Graph)
                    {
                        deviceGraphModels.Add(new PlatformGraphModel(graph.PlatformGraph, graph));
                    }
                }
                else if (Resource.SQLModel_Signal_DeviceType == device.DeviceType)
                {
                    deviceModels.Add(new SignalModel(device.SignalDevice, device));

                    foreach (Graph graph in device.Graph)
                    {
                        deviceGraphModels.Add(new SignalGraphModel(graph.SignalGraph, graph));
                    }
                }
                else if (Resource.SQLModel_Switch_DeviceType == device.DeviceType)
                {
                    deviceModels.Add(new SwitchModel(device.SwitchDevice, device));

                    foreach (Graph graph in device.Graph)
                    {
                        deviceGraphModels.Add(new SwitchGraphModel(graph.SwitchGraph, graph));
                    }
                }
                else if (Resource.SQLModel_TrackSegment_DeviceType == device.DeviceType)
                {
                    deviceModels.Add(new TrackSegmentModel(device.TrackSegmentDevice, device));

                    foreach (Graph graph in device.Graph)
                    {
                        deviceGraphModels.Add(new TrackSegmentGraphModel(graph.TrackSegmentGraph, graph));
                    }
                }
            }

            return true;
        }

        static public bool SaveProject(SQLModelDataContext dataContext, ICollection<DeviceBaseModel> deviceModels, ICollection<DeviceBaseGraphModel> deviceGraphModels)
        {
            if (!dataContext.DatabaseExists())
            {
                return false;
            }

            // 清除数据库数据
            dataContext.Device.DeleteAllOnSubmit(dataContext.Device);
            dataContext.Graph.DeleteAllOnSubmit(dataContext.Graph);
            dataContext.SubmitChanges();

            foreach (DeviceBaseModel deviceModel in deviceModels)
            {
                dataContext.Device.InsertOnSubmit(deviceModel.Device);

                if (deviceModel is AxleModel)
                {
                    dataContext.AxleDevice.InsertOnSubmit((deviceModel as AxleModel).Axle);
                }
                else if (deviceModel is MiscellaneousDeviceModel)
                {
                    dataContext.MiscellaneousDevice.InsertOnSubmit((deviceModel as MiscellaneousDeviceModel).MD);
                }
                else if (deviceModel is PlatformModel)
                {
                    dataContext.PlatformDevice.InsertOnSubmit((deviceModel as PlatformModel).Platform);
                }
                else if (deviceModel is SignalModel)
                {
                    dataContext.SignalDevice.InsertOnSubmit((deviceModel as SignalModel).Signal);
                }
                else if (deviceModel is SwitchModel)
                {
                    dataContext.SwitchDevice.InsertOnSubmit((deviceModel as SwitchModel).Switch);
                }
                else if (deviceModel is TrackSegmentModel)
                {
                    dataContext.TrackSegmentDevice.InsertOnSubmit((deviceModel as TrackSegmentModel).TrackSegment);
                }
                else
                {

                }
            }

            foreach (DeviceBaseGraphModel deviceGraphModel in deviceGraphModels)
            {
                dataContext.Graph.InsertOnSubmit(deviceGraphModel.Graph);

                if (deviceGraphModel is AxleGraphModel)
                {
                    dataContext.AxleGraph.InsertOnSubmit((deviceGraphModel as AxleGraphModel).Axle);
                }
                else if (deviceGraphModel is MiscellaneousDeviceGraphModel)
                {
                    dataContext.MiscellaneousDeviceGraph.InsertOnSubmit((deviceGraphModel as MiscellaneousDeviceGraphModel).MD);
                }
                else if (deviceGraphModel is PlatformGraphModel)
                {
                    dataContext.PlatformGraph.InsertOnSubmit((deviceGraphModel as PlatformGraphModel).Platform);
                }
                else if (deviceGraphModel is SignalGraphModel)
                {
                    dataContext.SignalGraph.InsertOnSubmit((deviceGraphModel as SignalGraphModel).Signal);
                }
                else if (deviceGraphModel is SwitchGraphModel)
                {
                    dataContext.SwitchGraph.InsertOnSubmit((deviceGraphModel as SwitchGraphModel).Switch);
                }
                else if (deviceGraphModel is TrackSegmentGraphModel)
                {
                    dataContext.TrackSegmentGraph.InsertOnSubmit((deviceGraphModel as TrackSegmentGraphModel).TrackSegment);
                }
                else
                {

                }
            }

            dataContext.SubmitChanges();

            return true;
        }
    }
}